package com.nl.lib_project.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.nl.lib_core.tool.extends.dp2px
import com.nl.lib_project.R

/** 作者: wangfei 创建时间: 2021/9/3 包名: com.nl.lib_project.view 类名: popuView */
open class PopView : View {

  /** 当前三角形的高度 */
  private var mTriangleHeight = 0F
  /** 三角形顶点的X轴坐标 */
  private var mTrianglePosition = 0F
  /** 画笔 */
  private val mPaint by lazy { Paint() }
  /** 边框路径 */
  private val mPath = Path()
  /** 圆角的大小 */
  private val mRadius = 30f
  // 三角顶点圆弧
  private val mTriangleTopRadius = 10f
  constructor(context: Context) : this(context, null)
  constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
  constructor(
      context: Context,
      attrs: AttributeSet?,
      defStyleAttr: Int
  ) : super(context, attrs, defStyleAttr) {
    // 获取属性
    val a = context.obtainStyledAttributes(attrs, R.styleable.PopView, 0, 0)
    mTriangleHeight = a.getDimension(R.styleable.PopView_popu_triangle_height, 0F)
    a.recycle()
    // 描边宽度
    mPaint.strokeWidth = context.dp2px(1f).toFloat()
    // 设置抗锯齿
    mPaint.isAntiAlias = true
    // 设置防抖动
    mPaint.isDither = true
  }

  override fun onDraw(canvas: Canvas?) {
    super.onDraw(canvas)
    val width = measuredWidth - 4f
    val height = measuredHeight - 4f
    canvas!!.save()
    mPath.reset()
    // 左上角起始位置
    mPath.moveTo(mRadius, mTriangleHeight)
    // 绘制三角
    // 移动到三角形左下角的位置
    mPath.lineTo(mTrianglePosition - mTriangleHeight, mTriangleHeight)
    // 三角形顶点
    mPath.lineTo(mTrianglePosition - mTriangleTopRadius, mTriangleTopRadius)
    mPath.quadTo(mTrianglePosition, 0f, mTrianglePosition + mTriangleTopRadius, mTriangleTopRadius)
    // 三角形右下角
    mPath.lineTo(mTrianglePosition + mTriangleHeight, mTriangleHeight)
    // 三角绘制完毕
    mPath.lineTo(width - mRadius, mTriangleHeight)
    // 右上方圆角
    mPath.arcTo(
        width - mRadius * 2,
        mTriangleHeight,
        width,
        mRadius * 2 + mTriangleHeight,
        -90f,
        90f,
        false)
    mPath.lineTo(width, height - mRadius)
    // 右下角圆弧
    mPath.arcTo(width - mRadius * 2, height - mRadius * 2, width, height, 0f, 90f, false)
    mPath.lineTo(mRadius, height)
    // 左下角圆弧
    mPath.arcTo(2f, height - mRadius * 2, mRadius * 2, height, 90f, 90f, false)
    mPath.lineTo(2f, mRadius * 2 + mTriangleHeight)
    // 左上角圆弧
    mPath.arcTo(2f, mTriangleHeight, mRadius * 2, mRadius * 2 + mTriangleHeight, 180f, 90f, false)
    // 路径绘制完毕
    mPaint.color = ContextCompat.getColor(context, R.color.second_level_solid)
    mPaint.style = Paint.Style.FILL
    canvas.drawPath(mPath, mPaint)
    mPaint.style = Paint.Style.STROKE
    mPaint.color = ContextCompat.getColor(context, R.color.second_level_border)
    canvas.drawPath(mPath, mPaint)
  }

  /**
   * 设置当前
   * @param position 设置当前三角形气泡起点位置
   */
  fun setTrianglePosition(position: Float) {
    mTrianglePosition = position
    invalidate()
  }
}
